import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have personRole requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/userInfo',
    component: () => import('@/views/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout, // 加入侧边栏与总布局Layout（主业务页面部分都要）
    redirect: '/home', // 实际跳转url
    children: [{
      path: 'home', // 若‘/xxx‘则是绝对路径，’xxx'是相对
      name: 'Home',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/form',
    name: 'Example',
    meta: { title: 'Example', icon: 'example' },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          },
          {
            path: 'menu1-4',
            component: () => import('@/views/nested/menu1/menu1-4'),
            name: 'Menu1-4',
            meta: { title: 'Menu1-4' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        meta: { title: 'menu2' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

export const asyncRoutes = [
  {
      path: '/personRole',
      component: Layout,
      redirect: '/personRole/userList',
      alwaysShow: true,
      name: 'PersonRole',
      meta: {
        title: '用户与权限',
        icon: 'user',
        roles: ['admin']
      },
      children: [
        {
          path: 'userList',
          component: () => import('@/views/personRole/userList'),
          name: 'UserListPermission',
          meta: { title: '用户管理列表' }
        },
        {
          path: 'role',
          component: () => import('@/views/personRole/role'),
          name: 'RolePermission',
          meta: {
            title: '权限管理列表',
            roles: ['admin']
          }
        }
      ]
  },

  {
    path: '/userDetail',
    component: Layout,
    redirect: '/userDetail/alumniList',
    alwaysShow: true,
    name: 'userDetail',
    meta: {
      title: '用户详细信息管理',
      icon: 'form',
      roles: ['admin']
    },
    children: [
      {
        path: 'alumniList',
        component: () => import('@/views/userDetail/alumni/alumniList'),
        name: 'alumniList',
        meta: { title: '校友信息管理列表' }
      },
      {
        path: 'stuList',
        component: () => import('@/views/userDetail/student/stuList'),
        name: 'stuList',
        meta: { title: '学生信息管理列表' }
      },
      {
        path: 'teacherList',
        component: () => import('@/views/userDetail/teacher/teacherList'),
        name: 'teacherList',
        meta: { title: '教师信息管理列表' }
      },
    ]
  },

  {
      path: '/files',
      component: Layout,
      redirect: '/files/list',
      name: 'Files',
      meta: {
        title: '文章/活动管理',
        icon: 'excel',
        roles: ['admin']
      },
      children: [
        {
          path: 'list',
          component: () => import('@/views/files/article-list'),
          name: 'ArticleList',
          meta: { title: '文章/活动列表', icon: 'list' }
        },
        {
          path: 'create',
          component: () => import('@/views/files/article-create'),
          name: 'CreateArticle',
          meta: { title: '文章/活动创建', icon: 'edit' }
        },
        {
          path: 'edit/:id(\\d+)',
          component: () => import('@/views/files/article-edit'),
          name: 'EditArticle',
          meta: { title: '文章/活动编辑', noCache: true, activeMenu: '/files/list' },
          hidden: true
        },
        {
          path: 'export-selected-files',
          component: () => import('@/views/files/select-files'),
          name: 'SelectFiles',
          meta: { title: '导出记录' }
        },
        {
          path: 'upload-files',
          component: () => import('@/views/files/upload-excel'),
          name: 'UploadExcel',
          meta: { title: '上传记录' },
          hidden: true
        }
      ]
    },

    {
      path: '/group',
      component: Layout,
      redirect: '/group/list',
      name: 'Group',
      meta: {
        title: '群组管理',
        icon: 'wechat',
        roles: ['admin']
      },
      children: [
        {
          path: 'list',
          name: 'GroupList',
          component: () => import('@/views/group/group-list'),
          meta: { title: '群组列表', icon: 'list' }
        },
        {
          path: 'create',
          name: 'CreateGroup',
          component: () => import('@/views/group/group-create'),
          meta: { title: '群组创建', icon: 'edit' }
        },
        {
          path: 'groupEdit/:id(\\d+)',
          name: 'GroupEdit',
          component: () => import('@/views/group/group-edit'),
          meta: { title: '群组成员', noCache: true, activeMenu: '/group/groupList' },
          hidden: true
        },
      ]
    },

    {
      path: '/mobile_userInfo',
      component: Layout,
      redirect: '/mobile_userInfo',
      meta: { roles: ['ordinary','alumni','student','teacher'] },
      children:[{
          path: 'userInfo',
          name: 'UserInfo',
          component: () => import('@/views/mobile/userInfo'),
          meta: {icon: 'user',title: '个人信息'}
        }]
    },
    {
      path: '/mobile_bindInfo',
      component: Layout,
      redirect: '/mobile_bindInfo',
      meta: { roles: ['ordinary'] },
      children:[{
        path: 'bindInfo',
        name: 'BindInfo',
        component: () => import('@/views/mobile/bind'),
        meta: {icon: 'edit',title: '信息绑定'}
      }]
    },
    {
      path: '/mobile_chat',
      component: Layout,
      redirect: '/mobile_chat',
      meta: { roles: ['alumni','student','teacher'] },
      children:[{
        path: 'groupChatRoom',
        name: 'GroupChatRoom',
        component: () => import('@/views/mobile/groupChatRoom'),
        meta: {icon: 'wechat',title: '群组聊天'}
      }]
    },
    {
      path: '/mobile_activity',
      component: Layout,
      redirect: '/mobile_activity/mobileActivityList',
      meta: { icon: 'nested',title: '在线活动',roles: ['alumni','student','teacher']},
      children:[{
          path: 'mobileActivityList',
          name: 'MobileActivityList',
          component: () => import('@/views/mobile/activity'),
          meta: {title: '活动列表' ,roles: ['alumni','student','teacher']}
        },
        {
          path: 'mobileCreateActivity',
          name: 'MobileCreateActivity',
          component: () => import('@/views/mobile/activity/createActivity'),
          meta: {title: '创建活动' ,roles: ['alumni','teacher']}
        },
      ]
    },
  ]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
